import '../model/paging_model.dart';
import '../model/result_model.dart';
import '../model/school_model.dart';
import '../model/semester_model.dart';
import '../utils/request.dart';

///学校API
class SchoolApi {
  SchoolApi._();

  static final _signleton = SchoolApi._();

  factory SchoolApi() => _signleton;

  ///分页查询
  Future<SchoolQueryRes> query(final SchoolQueryReq req) => request
      .getHandler(path: '/${req.tenantId}/school', params: req.toMap())
      .then((res) => SchoolQueryRes.fromModel(res));

  ///加载
  Future<SchoolInfoRes> info(final String id) => request
      .getHandler(path: '/school/$id')
      .then((res) => SchoolInfoRes.fromModel(res));

  ///新增
  Future<BaseModelRes> add(final SchoolAddReq req) =>
      request.postHandler(path: '/${req.tenantId}/school', data: req.toMap());

  ///修改
  Future<BaseModelRes> modify(final SchoolModifyReq req) =>
      request.putHandler(path: '/school/${req.id}', data: req.toMap());

  ///删除
  Future<BaseModelRes> delete(final String schoolId) {
    final path = '/school/$schoolId';
    return request.deleteHandler(path: path);
  }

  ///学校学年学期-加载集合
  Future<List<SemesterItemRes>> semestersBySchool(final String schoolId) =>
      request.getHandler(path: '/school/$schoolId/semesters').then((res) {
        final List<dynamic> items = res.toList;
        if (items.isNotEmpty) {
          return parseToRows(items, (row) => SemesterItemRes.fromMap(row));
        }
        return [];
      });

  ///学校学年学期-保存
  Future<BaseModelRes> saveSchoolSemesters(
    final String schoolId,
    final List<String> semesters,
  ) =>
      request.postHandler(
        path: '/school/$schoolId/semesters',
        data: {'semesters': semesters},
      );
}

///学校接口单例
final schoolApi = SchoolApi();
